﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UILibrary
{
    /// <summary>
    /// ReagentItem.xaml 的交互逻辑
    /// </summary>
    public partial class ReagentItem : UserControl
    {
        
        public ReagentItem()
        {
            InitializeComponent();
            DataContext = this;
        }



        public ReagentItemData Data
        {
            get { return (ReagentItemData)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(ReagentItemData), typeof(ReagentItem), new PropertyMetadata(null, (o, e) =>
            {
                //var x = 1;
            }));


        public double RotateAngle
        {
            get { return (double)GetValue(RotateAngleProperty); }
            set { SetValue(RotateAngleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RotateAngle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RotateAngleProperty =
            DependencyProperty.Register("RotateAngle", typeof(double), typeof(ReagentItem), new PropertyMetadata(0.0));



        public int Index
        {
            get { return (int)GetValue(IndexProperty); }
            set { SetValue(IndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Index.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register("Index", typeof(int), typeof(ReagentItem), new PropertyMetadata(0, PropertyChangedCallback));


        /// <summary>
        /// 角度
        /// </summary>
        public double Angle
        {
            get { return (double)GetValue(AngleProperty); }
            set
            {
                SetValue(AngleProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AngleProperty =
            DependencyProperty.Register("Angle", typeof(double), typeof(ReagentItem), new PropertyMetadata(30.0, PropertyChangedCallback));


        /// <summary>
        /// 间距角度
        /// </summary>
        public double Space
        {
            get { return (double)GetValue(SpaceProperty); }
            set
            {
                SetValue(SpaceProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Space.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SpaceProperty =
            DependencyProperty.Register("Space", typeof(double), typeof(ReagentItem), new PropertyMetadata(0.0));


        /// <summary>
        /// 外半径
        /// </summary>
        public double RadiusOuter
        {
            get { return (double)GetValue(RadiusOuterProperty); }
            set
            {
                SetValue(RadiusOuterProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for RadiusOuter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusOuterProperty =
            DependencyProperty.Register("RadiusOuter", typeof(double), typeof(ReagentItem), new PropertyMetadata(100.0, PropertyChangedCallback));


        /// <summary>
        /// 内半径
        /// </summary>
        public double RadiusInner
        {
            get { return (double)GetValue(RadiusInnerProperty); }
            set
            {
                SetValue(RadiusInnerProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for RadiusInner.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusInnerProperty =
            DependencyProperty.Register("RadiusInner", typeof(double), typeof(ReagentItem), new PropertyMetadata(0.0, PropertyChangedCallback));


        public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var item = (ReagentItem)d;
            if (item != null)
            {
                item.arc.AngleStart = item.Index*item.Angle;
                item.arc.AngleEnd = (item.Index+1) * item.Angle;
            }
        }

        /// <summary>
        /// 边框色彩Brush
        /// </summary>
        public Brush BorderColor
        {
            get { return (Brush)GetValue(BorderColorProperty); }
            set { SetValue(BorderColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BorderBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderColorProperty =
            DependencyProperty.Register("BorderColor", typeof(Brush), typeof(ReagentItem), new PropertyMetadata(new SolidColorBrush(Colors.Gray)));



        public double BorderWidth
        {
            get { return (double)GetValue(BorderWidthProperty); }   
            set { SetValue(BorderWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BorderWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderWidthProperty =
            DependencyProperty.Register("BorderWidth", typeof(double), typeof(ReagentItem), new PropertyMetadata(0.5));



        public DoubleCollection BorderDashArray
        {
            get { return (DoubleCollection)GetValue(BorderDashArrayProperty); }
            set { SetValue(BorderDashArrayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BorderDashArray.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderDashArrayProperty =
            DependencyProperty.Register("BorderDashArray", typeof(DoubleCollection), typeof(ReagentItem), new PropertyMetadata(null));




        public ReagentItemTypeStyle TypeStyle
        {
            get { return (ReagentItemTypeStyle)GetValue(TypeStyleProperty); }
            set { SetValue(TypeStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TypeStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TypeStyleProperty =
            DependencyProperty.Register("TypeStyle", typeof(ReagentItemTypeStyle), typeof(ReagentItem), new PropertyMetadata(
               null
                , (o, e) =>
                {
                    var item = (ReagentItem)o;
                    if (item != null && item.Style!=null)
                    {
                        Color color = (Color)ColorConverter.ConvertFromString(item.TypeStyle.Color.ToString());
                        var newColor = Color.Add(color, Color.FromRgb(80, 80, 80));
                        item.BgBrush = new SolidColorBrush(newColor);
                    }
                }));





        public Brush BgBrush
        {
            get { return (Brush)GetValue(BgBrushProperty); }
            set { SetValue(BgBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BgBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BgBrushProperty =
            DependencyProperty.Register("BgBrush", typeof(Brush), typeof(ReagentItem), new PropertyMetadata(new SolidColorBrush(Colors.Gray)));

        /// <summary>
        /// 选中
        /// </summary>
        public bool IsSelected
        {
            get { return (bool)GetValue(SelectedProperty); }
            set { SetValue(SelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Selected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ReagentItem), new PropertyMetadata(false));


        /// <summary>
        /// 标记
        /// </summary>
        public bool IsSigned
        {
            get { return (bool)GetValue(IsSignedProperty); }
            set { SetValue(IsSignedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSign.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSignedProperty =
            DependencyProperty.Register("IsSigned", typeof(bool), typeof(ReagentItem), new PropertyMetadata(false));




        /// <summary>
        /// 名称Label,显示在扇形中心
        /// </summary>
        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Label.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(ReagentItem), new PropertyMetadata(""));


    }


    public class ReagentItemData: INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private int index { get; set; }
        /// <summary>
        /// 序列
        /// </summary>
        public int Index
        {
            get { return index; }
            set {
                index = value;
                NotifyPropertyChanged(); 
            }
        }
        private string content { get; set; }
        /// <summary>
        /// 序列
        /// </summary>
        public string Content
        {
            get { return content; }
            set
            {
                content = value;
                NotifyPropertyChanged();
            }
        }

        private string key { get; set; }

        public string DataKey
        {
            get { return key; }
            set { 
                key = value;
                NotifyPropertyChanged();
            }
        }

        private ReagentItemState state { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public ReagentItemState State
        {
            get { return state; }
            set { state = value;
                NotifyPropertyChanged();
            }
        }
        private string type { get; set; }

        public string Type
        {
            get { return type; }
            set {
                type = value;
                NotifyPropertyChanged();
            }
        }
        /// <summary>
        /// 次数
        /// </summary>
        private int count;

        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        /// <summary>
        /// 剩余百分比
        /// </summary>
        private double percent { get; set; }

        public double Percent
        {
            get { return percent; }
            set
            {
                percent = value;
                NotifyPropertyChanged();
            }
        }

    }
    public class ReagentItemData<T> : ReagentItemData where T : class
    {
        private T orginData { get; set; }

        public T OrginData
        {
            get { return orginData; }
            set
            {
                orginData = value;
                NotifyPropertyChanged();
            }
        }
    }
    public enum ReagentItemState
    {
           Normal =1,
           Low=2,
           Null=3,
    }
    public enum ReagentItemType
    {
        禁用 = 0,
        无 = 1,
        试剂 = 2,
        水 = 3,
        酸 = 4,
        碱 = 5,
    }
}
